#if (FEATURE_MODE == 0)
/**
 @file sys_usw_npm.h

 @date 2014-10-28

 @version v3.0

Macro, data structure for system common operations

*/

#ifndef _SYS_USW_NPM
#define _SYS_USW_NPM
#ifdef __cplusplus
extern "C" {
#endif

#include "sal.h"
#include "ctc_npm.h"
#include "ctc_vector.h"

/***************************************************************
 *
 *  Defines and Macros
 *
 ***************************************************************/
#define SYS_NPM_LITE_SESSION_CONVERT(session_id)      (session_id &((1 << CTC_NPM_SESSION_ID_LEN)-1))

/***************************************************************
 *
 *  Functions
 *
 ***************************************************************/
#define SYS_NPM_MAX_SESSION_NUM 32
#define SYS_NPM_MAX_TWAMP_LITE_SESSION_NUM 512
#define SYS_NPM_DECIMAL_VALUE_FOR_PRECISION 0x8AC72304

#define SYS_NPM_DBG_OUT(level, FMT, ...) \
    do { \
        CTC_DEBUG_OUT(npm, npm, NPM_SYS, level, FMT, ##__VA_ARGS__); \
    } while (0)

#define SYS_NPM_MAX_SESSION_CHECK(max, id) \
    do { \
        if (id >= max){ \
            return CTC_E_INVALID_PARAM; } \
    } while (0)

#define SYS_NPM_1M_BYTES 125000     /*(1000*1000/8)*/
#define SYS_IPG_MAX_VALUE 63
#define  SYS_NPM_VEC_BLOCK_NUM 4
#define SYS_NPM_LITE_VEC_BLOCK_NUM 16
#define SYS_NPM_TS_TO_NS(NTP, TS) (NTP ? (TS *232 / 1000) : TS)
#define SYS_NPM_INIT_CHECK(lchip) \
    do { \
        LCHIP_CHECK(lchip); \
        if (NULL == g_npm_master[lchip]) \
        { \
            return CTC_E_NOT_INIT; \
        } \
    } while (0)

#define NPM_LOCK(lchip) \
    do { \
        if (g_npm_master[lchip]->npm_mutex) \
        { \
            sal_mutex_lock(g_npm_master[lchip]->npm_mutex); \
        } \
    } while (0)

#define NPM_UNLOCK(lchip) \
    do { \
        if (g_npm_master[lchip]->npm_mutex) \
        { \
            sal_mutex_unlock(g_npm_master[lchip]->npm_mutex); \
        } \
    } while (0)
enum sys_npm_opf_type_e
{
    SYS_NPM_OPF_TYPE_DMM_TWAMP_LITE, /* TMM ...*/
    SYS_NPM_OPF_TYPE_MAX
};
typedef enum sys_npm_opf_type_e sys_npm_opf_type_t;
struct sys_npm_lite_stats_s
{
    uint64 rx_fcl_all;
    uint8 max_session;
    uint32 far_min_jitter;
    uint32 far_max_jitter;
    uint32 near_min_jitter;
    uint32 near_max_jitter;
    uint32 two_way_min_jitter;
    uint32 far_total_jitter_all;
    uint32 near_total_jitter_all;
    uint32 two_way_total_jitter_all;
    uint32 two_way_max_jitter;
};
typedef struct sys_npm_lite_stats_s sys_npm_lite_stats_t;

struct sys_npm_common_stats_s
{
    uint64 total_delay_all;
    uint64 tx_pkts_all;
    uint64 rx_pkts_all;
    uint64 max_delay;
    uint64 min_delay;
    uint64 max_jitter;
    uint64 min_jitter;
    uint64 total_jitter_all;
    uint64 disorder_pkts;
    uint64 last_ts;
    uint8   is_ntp_ts;
};
typedef struct sys_npm_common_stats_s sys_npm_common_stats_t;

struct sys_npm_lite_session_s
{
    uint8 is_npm;
    sys_npm_common_stats_t session_stats;
    sys_npm_lite_stats_t lite_stats;
    sys_npm_common_stats_t session_old_stats;
    sys_npm_lite_stats_t lite_old_stats;
    uint32 mep_idx;
    uint32 twamp_idx;
    uint32 session_id;
    uint32 ma_idx;
    uint32 ts_format:1;
    uint32 mode:2;
    uint32 is_twamp:1;
    uint32 is_ipv6:1;
    uint32 synced:1;
    uint32 rsv:26;
    
};
typedef struct sys_npm_lite_session_s sys_npm_lite_session_t;

struct sys_npm_session_s
{
    sys_npm_common_stats_t session_stats;
    uint32 session_id;
    uint32 mep_idx;
    uint8 continuous_ses_cnt;
    uint8 continuous_non_ses_cnt;
    uint64 ses_cnt;
    uint64 non_ses_cnt;
    uint8 flr_for_ses;
    uint64 max_rx_ir;
    uint64 min_rx_ir;
    uint64 seconds;
    uint8 in_avl_period;
    uint32 avl_period_thrd;
    uint64 avl_periods;
    uint64 unavl_periods;
    uint64 total_far_delay_all;
    uint64 tx_bytes_all;
    uint64 rx_bytes_all;
    uint32 last_disorder_pkts;
};
typedef struct sys_npm_session_s sys_npm_session_t;

struct sys_npm_master_s
{
    uint32 config_dmm_bitmap;
    uint32 tx_dmm_en_bitmap;
    sal_mutex_t*  npm_mutex;
    ctc_vector_t* session_lite;
    ctc_vector_t* session;
    uint8  npm_opf_type;
    ctc_hash_t *p_npm_im_flow_hash;
    uint8  opf_type_im_loss;   /* range: 0~4095 */
    uint8  opf_type_im_delay;   /* range: 0~4095 */
	uint8 ecpu_npm_im_en;
    ctc_npm_im_loss_t *loss_flow;
    uint64 *tx_pkts;
    uint64 *rx_pkts;
    uint64 *rx_bytes;
    uint32 src_node_fp_id[SYS_IM_MAX_TYPE];
    uint32 src_node_delay_fp_id[SYS_IM_MAX_TYPE];
    uint32 sink_node_fp_id[SYS_IM_MAX_TYPE];
    uint8 im_protocol;
};
typedef struct sys_npm_master_s sys_npm_master_t;

struct sys_npm_im_flow_hash_s
{
    /*key*/
    uint32 flow_id;

    /*action*/
    uint32 flow_type :2;
    uint32 loss_prof_id:3;
    uint32 fih_type:2;
    uint32 delay_prof_id:12;
    uint32 delay_prof_id_valid:1;
    uint32 type:2;
    uint32 l_valid:1;
    uint32 l_flag:1;
    uint32 d_flag:1;
    uint32 ip_im_type:2;
    uint32 acl_bind_en:1;
    uint32 d_valid:5;
    uint32 rsv:3;

    uint32 stats_id;
    uint32 metadata:15;
    uint32 rsv1:17;
    uint32 igs_stats_ptr;
    uint32 egs_stats_ptr;

    uint32 acl_entry_id;
};
typedef struct sys_npm_im_flow_hash_s sys_npm_im_flow_hash_t;

/* Not support */
enum ctc_npm_im_type_useless_e
{
    CTC_NPM_IM_TYPE_IPV4 = 1,   /* [AT] IPV4 IOAM */
    CTC_NPM_IM_TYPE_IPV6        /* [AT] IPV6 IOAM */
};
typedef enum ctc_npm_im_type_useless_e ctc_npm_im_type_useless_t;

extern sys_npm_master_t* g_npm_master[CTC_MAX_LOCAL_CHIP_NUM_PP];
extern int32
sys_usw_npm_set_config(uint8 lchip, ctc_npm_cfg_t* p_cfg);

extern int32
sys_usw_npm_get_config(uint8 lchip, ctc_npm_cfg_t* p_cfg);

extern int32
sys_usw_npm_set_transmit_en(uint8 lchip, uint32 session_id, uint8 enable);

extern int32
sys_usw_npm_get_stats(uint8 lchip, uint32 session_id, ctc_npm_stats_t* p_stats);

extern int32
sys_usw_npm_set_global_config(uint8 lchip, ctc_npm_global_cfg_t* p_npm);

extern int32
sys_usw_npm_get_global_config(uint8 lchip, ctc_npm_global_cfg_t* p_npm);

extern int32
sys_usw_npm_clear_stats(uint8 lchip, uint32 session_id);

extern int32
sys_usw_npm_binding_mep(uint8 lchip, uint32 session_id, uint32 mep_idx, uint32* twamp_idx);
extern int32
sys_usw_npm_unbinding_mep(uint8 lchip, uint32 session_id);
extern int32
sys_usw_npm_im_convert_flow_id(uint8 lchip, uint32 flow_id, uint32* flow_id_converted);
extern int32
sys_usw_npm_im_recovery_flow_id(uint8 lchip, uint32 flow_id_converted, uint32* flow_id);
extern int32
sys_usw_npm_im_create_flow(uint8 lchip, ctc_npm_im_flow_t* p_flow);
extern int32
sys_usw_npm_im_destory_flow(uint8 lchip, uint32 flow_id);
extern int32
sys_usw_npm_im_set_flow_property(uint8 lchip, ctc_npm_im_flow_property_t* p_npm_im_pro);
extern int32
sys_usw_npm_im_get_flow_property(uint8 lchip, ctc_npm_im_flow_property_t* p_npm_im_pro);
extern int32
sys_usw_npm_im_set_loss_profile_interval(uint8 lchip, void*p_loss_prof);
extern int32
sys_usw_npm_im_get_loss_profile_interval(uint8 lchip, void*p_loss_prof);
extern int32
sys_usw_npm_init(uint8 lchip, void* npm_global_cfg);

extern int32
sys_usw_npm_deinit(uint8 lchip);
#ifdef __cplusplus
}
#endif

#endif

#endif

